//============================================================================
// Name        : p1_main.cpp
// Author      : William Eka Putra, Ross Dixon, Erick Chaves
// Version     : 1.0.0
// Licnese     : New BSD License
// Description : C++ project part I for CSCE 121 and IF4093 
//============================================================================
//

#include "Menu.h"

using namespace std;

int main()
{
  try {
    Menu m;
    int option;
    
    cout << "\nWelcome to the Restaraunt Order Management System(ROMS)\n";	//RCD A.2
    cout << "Version A\n";	//RCD A.2
    cout << "By Team 3: William, Erick, and Ross\n";	//RCD A.2
    
    /*********************************************************************/
    /*Created or Modified by WEP*/
    /*Added string initiation for exit confirmation*/
    /*********************************************************************/
    string exitConfirmation;
  
    m.display_main_menu();
    do {
      cout << "> ";
      while (!(cin >> option) || option < Menu::Info || option > Menu::Exit) {
	if (cin.fail()) {    // we found something that wasn't an integer 
	  cin.clear();      // enable us to look at the characters
	  char ch;
	  while (cin>>ch && !isdigit(ch)) {
	    cerr << "Invalid input; please try again" << endl;
	    cout << "> ";
	  }
	  cin.unget();
	} else {  // option < Menu::Info || option > Menu::Exit */
	  cerr << "Invalid input; please try again" << endl;
	  cout << "> ";
	}
      } 
      
      switch(option) { 
      case Menu::Info:
	m.display_info();//display assignment info & your names plus "Project 1"
	break;
      case Menu::Read:
	try {
	  m.read();
	} catch (const Menu::InvalidFile& excp) {
	  cerr << excp.what() << endl;
	} catch (const Menu::InvalidData& excp) {
	  cerr << excp.what() << endl;
	}
	break;
      case Menu::Find:
	m.find();
	break;
      case Menu::Show:
	m.show();
	break;
      case Menu::Update:
	m.update();
	break;

	/*********************************************************************/
	/*Created or Modified by WEP*/
	/*Added case for option Exit*/
	/*********************************************************************/
      case Menu::Exit:
	cout << "Are you sure want to exit? (Y/N) : ";
	cin >> exitConfirmation;
	while (exitConfirmation.compare("Y") != 0 && exitConfirmation.compare("y") != 0 && exitConfirmation.compare("N") != 0 && exitConfirmation.compare("n") != 0)
	  {
	    cout << "Invalid input; please try again." << endl;
	    cout << "Please type Y for YES or N for NO" << endl;
	    cout << "> ";
	    cin >> exitConfirmation;
	  }
	break;
      }
      
      m.display_main_menu();
    } 
    /*********************************************************************/
    /*Created or Modified by WEP*/
    /*Changed the iteration cause, if Yes, regardless of the case (Y/y) then exit*/
    /*********************************************************************/
    //while (option != Menu::Exit);
    while (exitConfirmation.compare("Y") != 0 && exitConfirmation.compare("y") != 0);
    m.write_orders();	//RCD D.1
    m.write_recipes();	//RCD D.1
    m.write_catmenu();
    return 0;
  }
  catch (exception& e) {
    cerr << e.what() << endl;   
  }
  catch (...) {
    cerr << "unknown exception\n";
  }
}
